package com.building.service.impl;

import com.building.dao.DormitoryDao;
import com.building.entity.Dormitory;
import com.building.entity.Liverecord;
import com.building.dao.LiverecordDao;
import com.building.response.ServerResponse;
import com.building.service.LiverecordService;
import com.building.sql.LiveRecordSql;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;

import javax.annotation.Resource;
import java.util.*;

/**
 * (Liverecord)表服务实现类
 *
 * @author TianCai
 * @since 2023-01-14 15:07:34
 */
@Service("liverecordService")
public class LiverecordServiceImpl implements LiverecordService {
    @Resource
    private LiverecordDao liverecordDao;
    @Resource
    private DormitoryDao dormitoryDao;

    /**
     * 通过ID查询单条数据
     *
     * @param lId 主键
     * @return 实例对象
     */
    @Override
    public Liverecord queryById(Integer lId) {
        return this.liverecordDao.queryById(lId);
    }

    /**
     * 分页查询
     *
     * @param liverecord 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */
    @Override
    public Page<Liverecord> queryByPage(Liverecord liverecord, PageRequest pageRequest) {
        long total = this.liverecordDao.count(liverecord);
        return new PageImpl<>(this.liverecordDao.queryAllByLimit(liverecord, pageRequest), pageRequest, total);
    }

    /**
     * 新增数据
     *
     * @param liverecord 实例对象
     * @return 实例对象
     */
    @Override
    public Liverecord insert(Liverecord liverecord) {
        this.liverecordDao.insert(liverecord);
        return liverecord;
    }

    /**
     * 修改数据
     *
     * @param liverecord 实例对象
     * @return 实例对象
     */
    @Override
    public Liverecord update(Liverecord liverecord) {
        this.liverecordDao.update(liverecord);
        return this.queryById(liverecord.getLId());
    }

    /**
     * 通过主键删除数据
     *
     * @param lId 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer lId) {
        return this.liverecordDao.deleteById(lId) > 0;
    }

    @Override
    public ServerResponse<PageInfo<LiveRecordSql>> getPageInfoByName(Integer pageNum, Integer pageSize, String name) {
        PageHelper.startPage(pageNum, pageSize);
        List<LiveRecordSql> liveRecordSqls = this.liverecordDao.queryAllByName(name);
        PageInfo<LiveRecordSql> liveRecordSqlPageInfo = new PageInfo<>(liveRecordSqls);
        int size = liveRecordSqlPageInfo.getList().size();
        if (size==0) return ServerResponse.createByErrorMessage("暂无数据!换个关键词试试吧!");
        return ServerResponse.createBySuccess("查询成功,共"+size+"条数据",liveRecordSqlPageInfo);
    }

    @Override
    public ServerResponse<PageInfo<LiveRecordSql>> getPageInfo(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<LiveRecordSql> liveRecordSqls = this.liverecordDao.queryAll();
        PageInfo<LiveRecordSql> liveRecordSqlPageInfo = new PageInfo<>(liveRecordSqls);
        int size = liveRecordSqlPageInfo.getList().size();
        if (size==0) return ServerResponse.createByErrorMessage("暂无数据!");
        return ServerResponse.createBySuccess("查询成功,共"+size+"条数据",liveRecordSqlPageInfo);
    }

    @Override
    public ServerResponse<Liverecord> addLive(Integer sid, Integer did) {
        Dormitory dormitory = dormitoryDao.queryById(did);
        if (dormitory==null) return  ServerResponse.createByErrorMessage("宿舍信息不存在!");
        Liverecord liverecord = new Liverecord(null,new Date(),did,sid,new Date(),new Date(),1,true);
        int insert = this.liverecordDao.insert(liverecord);
        if (insert!=1) return ServerResponse.createByErrorMessage("新增失败请检查服务器!");
        if (dormitory.getLivedNumber() < dormitory.getMaxNumber()){
            dormitory.setLivedNumber(dormitory.getLivedNumber()+1);
            dormitory.setUpdateTime(new Date());
            dormitory.setVersion(dormitory.getVersion()+1);
            dormitoryDao.update(dormitory);
            return ServerResponse.createBySuccess("新增成功",liverecord);
        }else{
            return ServerResponse.createByErrorMessage("宿舍已满员!");
        }
    }

    @Override
    public ServerResponse<Liverecord> updateLive(Integer id, Integer sid, Integer did) {
        Liverecord liverecord = this.liverecordDao.queryById(id);
        if (liverecord == null) return  ServerResponse.createByErrorMessage("入住信息信息不存在!");
        Integer dId = liverecord.getDId();
        Dormitory dormitory1 = dormitoryDao.queryById(dId);//原来宿舍的人删除
        dormitory1.setLivedNumber(dormitory1.getLivedNumber()-1);//人数减一
        dormitory1.setUpdateTime(new Date());
        dormitory1.setVersion(dormitory1.getVersion()+1);
        dormitoryDao.update(dormitory1);//修改旧宿舍信息

        Dormitory dormitory = dormitoryDao.queryById(did);//新宿舍
        if (dormitory.getLivedNumber() < dormitory.getMaxNumber()){//入住人数小于最大的时候才可以添加进去
            dormitory.setLivedNumber(dormitory.getLivedNumber()+1);//原来基础上加1
            dormitory.setUpdateTime(new Date());
            dormitoryDao.update(dormitory);
            liverecord.setSId(sid);
            liverecord.setSId(did);
            liverecord.setUpdateTime(new Date());
            liverecord.setVersion(liverecord.getVersion()+1);
            int update = liverecordDao.update(liverecord);
            if (update!=1) return ServerResponse.createByErrorMessage("修改失败请假查服务器!");
            return ServerResponse.createBySuccess("修改成功",liverecord);
        }else{
            return ServerResponse.createByErrorMessage("宿舍已满员,修改失败!");
        }
    }

    @Override
    public ServerResponse<Liverecord> deleteLive(Integer id) {
        Liverecord liverecord = this.liverecordDao.queryById(id);
        Dormitory dormitory = this.dormitoryDao.queryById(liverecord.getDId());
        dormitory.setLivedNumber(dormitory.getLivedNumber()-1);
        dormitory.setUpdateTime(new Date());
        dormitory.setVersion(dormitory.getVersion()+1);
        this.dormitoryDao.update(dormitory);//修改原来的宿舍信息，因为这条记录已经被删除

        liverecord.setStatus(false);
        liverecord.setUpdateTime(new Date());
        liverecord.setVersion(liverecord.getVersion()+1);
        int update = liverecordDao.update(liverecord);
        if (update!=1) return ServerResponse.createByErrorMessage("修改失败请检查服务器！");
        return ServerResponse.createBySuccess("该记录已经被删除",liverecord);
    }


}
